package com.foreknow.service.Impl;

import com.foreknow.dao.DeliveryAddressDao;
import com.foreknow.dao.impl.DeliveryAddressDaoImpl;
import com.foreknow.po.DeliveryAddress;
import com.foreknow.service.DeliveryAddressService;
import com.foreknow.util.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeliveryAddressServiceImpl implements DeliveryAddressService {
    @Override
    public List<DeliveryAddress> listDeliveryAddressByUserId(String userId) {
        DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
        List<DeliveryAddress> list = new ArrayList<>();
        try {

            list = dao.listDeliveryAddressByUserId(userId);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return list;
    }

    @Override
    public DeliveryAddress getDeliveryAddressById(int daId) {
        DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
        DeliveryAddress deliveryAddress = new DeliveryAddress();
        try {
            deliveryAddress = dao.getDeliveryAddressById(daId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return deliveryAddress;
    }

    @Override
    public Integer saveDeliveryAddress(String contactName, int contactSex, String contactTel, String address, String userId) {
        DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
        int result = 0;
        try {
            DBUtil.beginTransaction();
            result = dao.saveDeliveryAddress(contactName,contactSex,contactTel,address,userId);
            DBUtil.commitTransaction();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return result;
    }

    @Override
    public Integer updateDeliveryAddress(int daId, String contactName, int contactSex, String contactTel, String address) {
        DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
        int result = 0;
        try {
            DBUtil.beginTransaction();
            result = dao.updateDeliveryAddress(daId,contactName,contactSex,contactTel,address);
            DBUtil.commitTransaction();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return result;
    }

    @Override
    public Integer removeDeliveryAddress(int daId) {
        DeliveryAddressDao dao = new DeliveryAddressDaoImpl();
        int result = 0;
        try {
            DBUtil.beginTransaction();
            result = dao.removeDeliveryAddress(daId);
            DBUtil.commitTransaction();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return result;
    }

    public static void main(String[] args) {
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
//        List<DeliveryAddress> list = service.listDeliveryAddressByUserId("11111111111");
//        for (DeliveryAddress d:list){
//            System.out.println(d.getContactName());
//        }
        DeliveryAddress deliveryAddress = service.getDeliveryAddressById(14);
        System.out.println(deliveryAddress.getContactName());
    }
}
